<?php
/**
 * User:chendenan
 * Created On 2022/3/14 0014 21:06
 */

namespace app\api\controller;

use app\api\controller\AuthBase;
use app\common\business\User as UserBus;

class User extends AuthBase{
    public function index(){
        $user = (new UserBus())->getNormalUserById($this->userId);
        $resultUser = [
            'id' => $user['id'],
            'username' => $user['username'],
            'sex' => $user['sex'],
        ];
        return show("ok",config("status.success"),$resultUser);
    }
    public function update(){
        $username = input("param.username","","trim");
        $sex = input("param.sex","","intval");
        $data = [
            'username' => $username,
            'sex' => $sex,
            'user_count' => 1,
        ];
        $validate = (new \app\api\validate\User())->scene("update_user");
        if(!$validate->check($data)){
            return show($validate->getError(),config("status.error"));
        }
        $userBusObj = new UserBus();
        $user = $userBusObj->update($this->userId,$data);
        if(!$user){
            return show("更新失败",config("status.error"));
        }
        $redisData = [
            'id' => $this->userId,
            'username' => $username,
        ];
        cache(config("redis.token_pre").$this->accessToken,$redisData);
        return show("ok",1);
    }
}